import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:kxy_flutter_app/model/work_history.dart';

class WorkHistoryItemWidget extends StatelessWidget {
  final WorkHistory workHistory;
  final int index;
  final bool isLatest;
  WorkHistoryItemWidget({
    Key key,
    @required this.workHistory,
    @required this.index,
    this.isLatest = false,
  }) : super(key: key);

  final colors = [
    Colors.orangeAccent,
    Colors.blue,
    Colors.brown,
    Colors.cyan,
    Colors.green,
    Colors.grey,
    Colors.indigo,
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      child: timelineItem(context),
    );
  }

  Widget timelineItem(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          timelineHeader(workHistory.place, workHistory.craft),
          timelineBody(workHistory.startTime, workHistory.endTime, isLatest),
        ],
      ),
    );
  }

  Widget timelineHeader(String place, String craft) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Container(
          decoration: BoxDecoration(
            color: getColor(),
            shape: BoxShape.circle,
          ),
          width: 10,
          height: 10,
        ),
        Container(width: 20),
        Text(
          place,
          style: TextStyle(
            color: Colors.black,
            fontSize: 15,
            fontWeight: FontWeight.bold,
          ),
          overflow: TextOverflow.ellipsis,
        ),
        Container(
          width: 3,
          height: 3,
          decoration: BoxDecoration(
            color: Colors.black54,
            borderRadius: BorderRadius.circular(15),
          ),
          margin: EdgeInsets.symmetric(horizontal: 10),
        ),
        Expanded(
          child: Text(
            craft,
            style: TextStyle(
              color: Colors.black,
              fontSize: 15,
              fontWeight: FontWeight.bold,
            ),
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ],
    );
  }

  Widget timelineBody(DateTime start, DateTime end, bool isLatest) {
    var format = new DateFormat('yyyy年M月');
    var style = TextStyle(
      fontSize: 12,
      color: Colors.black54,
    );
    return Container(
      margin: EdgeInsets.only(left: 5),
      padding: EdgeInsets.fromLTRB(25, 5, 0, 25),
      decoration: isLatest
          ? null
          : BoxDecoration(
              border: Border(
                left: BorderSide(
                  color: Colors.black12,
                  width: 1,
                ),
              ),
            ),
      child: Row(
        children: [
          Text(format.format(start ?? DateTime.parse('0001-01-01')), style: style),
          Text(' - ', style: style),
          Text(format.format(end ?? DateTime.parse('0001-01-01')), style: style),
        ],
      ),
    );
  }

  Color getColor() {
    if (index >= colors.length) {
      return colors[index - colors.length];
    }
    return colors[index];
  }
}
